﻿#ifndef GdiPlusRender_H_
#define GdiPlusRender_H_

#pragma once
#include"StdAfx.h"
#include"TException.hpp"
#include<GdiPlus.h>
using namespace Gdiplus;

/// <summary>
/// 使用GDI之前必须创建该类指针,一个窗口一个
/// </summary>
class GdiPlusRender {
public:
	/// <summary>
	/// 以duilib的形式加载一张图片,
	/// </summary>
	/// <param name="_imgName">图片路径名</param>
	/// <param name="_pm"></param>
	/// <returns>如果加载失败返回NULL</returns>
	static Gdiplus::Bitmap* DuiLoadImage(LPCTSTR _imgName, CPaintManagerUI* _pm) {
		EXDEF
			const TImageInfo* tm = _pm->GetImageString(_imgName);

		if (tm == NULL)
		{

			EX_MESSAGEBOX(0, "图片不存在请检查")
				return NULL;
		}
		BITMAP bm;
		Gdiplus::Bitmap* gbm = NULL;
		//获取HBITMAP实际图像数据指针
		GetObject(tm->hBitmap, sizeof(BITMAP), &bm);
		unsigned char* bp = (unsigned char*)bm.bmBits;

		//获取gdi图像数据实际指针
		gbm = new Gdiplus::Bitmap(bm.bmWidth, bm.bmHeight);

		Gdiplus::Rect rc(0, 0, bm.bmWidth, bm.bmHeight);
		Gdiplus::BitmapData _BitmapData;
		gbm->LockBits(&rc, Gdiplus::ImageLockModeWrite, PixelFormat32bppARGB, &_BitmapData);
		unsigned char* gbmp = (unsigned char*)_BitmapData.Scan0;
		//拷贝HBITMAP图像实际数据到GDIbitmap
		memcpy(gbmp, bp, bm.bmWidthBytes * bm.bmHeight);
		gbm->UnlockBits(&_BitmapData);

		return gbm;
	};
	/// <summary>
	/// 将16进制的字符串转换成DWORD比如字符串"0xff000001"->16进制的0xff000001
	/// </summary>
	/// <param name="pstrValue"></param>
	/// <returns></returns>
	static DWORD HexStrToDWORD(LPCTSTR pstrValue) {
		LPTSTR pstr = NULL;
		return _tcstoul(pstrValue, &pstr, 16);
	};

	// 绘制及填充圆角矩形
	static	void DrawRoundRectangle(HDC hDC, float x, float y, float width, float height, float arcSize, float lineWidth, Gdiplus::Color lineColor, bool fillPath, Gdiplus::Color fillColor)
	{
		float arcDiameter = arcSize * 2;
		// 创建GDI+对象
		Gdiplus::Graphics  g(hDC);
		//设置画图时的滤波模式为消除锯齿现象
		g.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);

		// 绘图路径
		Gdiplus::GraphicsPath roundRectPath;

		// 保存绘图路径
		roundRectPath.AddLine(x + arcSize, y, x + width - arcSize, y);  // 顶部横线
		roundRectPath.AddArc(x + width - arcDiameter, y, arcDiameter, arcDiameter, 270, 90); // 右上圆角

		roundRectPath.AddLine(x + width, y + arcSize, x + width, y + height - arcSize);  // 右侧竖线
		roundRectPath.AddArc(x + width - arcDiameter, y + height - arcDiameter, arcDiameter, arcDiameter, 0, 90); // 右下圆角

		roundRectPath.AddLine(x + width - arcSize, y + height, x + arcSize, y + height);  // 底部横线
		roundRectPath.AddArc(x, y + height - arcDiameter, arcDiameter, arcDiameter, 90, 90); // 左下圆角

		roundRectPath.AddLine(x, y + height - arcSize, x, y + arcSize);  // 左侧竖线
		roundRectPath.AddArc(x, y, arcDiameter, arcDiameter, 180, 90); // 左上圆角

		//创建画笔
		Gdiplus::Pen pen(lineColor, lineWidth);
		// 绘制矩形
		g.DrawPath(&pen, &roundRectPath);

		// 是否填充
		if (fillPath) {
			if (fillColor.GetAlpha() == 0) {
				fillColor = lineColor; // 若未指定填充色，则用线条色填充
			}

			// 创建画刷
			Gdiplus::SolidBrush brush(fillColor);

			// 填充
			g.FillPath(&brush, &roundRectPath);
		}
	}

public:
	GdiPlusRender() {
		Gdiplus::GdiplusStartupInput StartupInput;
		GdiplusStartup(&m_gdiplusToken, &StartupInput, NULL);

	}
	~GdiPlusRender() {
		Gdiplus::GdiplusShutdown(m_gdiplusToken);

	}
private:
	ULONG_PTR m_gdiplusToken;

};
class TRect {

public:
	Rect m_rc;
	TRect() {
		m_rc.X = 0;
		m_rc.Y = 0;
		m_rc.Width = 0;
		m_rc.Height = 0;
	};
	TRect(RECT _rc) {
		m_rc.X = _rc.left;
		m_rc.Y = _rc.top;
		m_rc.Width = _rc.right - _rc.left;
		m_rc.Height = _rc.bottom - _rc.top;
	};
	TRect(int _x, int _y, int _w, int _h) {
		m_rc.X = _x;
		m_rc.Y = _y;
		m_rc.Width = _w;
		m_rc.Height = _h;
	};

	RECT ToRECT() {
		RECT orc;
		orc.left = m_rc.GetLeft();
		orc.top = m_rc.GetTop();
		orc.right = m_rc.GetRight();
		orc.bottom = m_rc.GetBottom();
		return orc;
	};


};
/// <summary>
/// 圆角矩形路径
/// </summary>
class CGraphicsRoundRectPath : public GraphicsPath
{

public:
	CGraphicsRoundRectPath() {
	};
	CGraphicsRoundRectPath(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY) {

		AddRoundRect(x, y, width, height, cornerX, cornerY);
	};

public:
	void AddRoundRect(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY) {
		INT elWid = 2 * cornerX;
		INT elHei = 2 * cornerY;

		AddArc(x, y, elWid, elHei, 180, 90); // 左上角圆弧
		AddLine(x + cornerX, y, x + width - cornerX, y); // 上边

		AddArc(x + width - elWid, y, elWid, elHei, 270, 90); // 右上角圆弧
		AddLine(x + width, y + cornerY, x + width, y + height - cornerY);// 右边

		AddArc(x + width - elWid, y + height - elHei, elWid, elHei, 0, 90); // 右下角圆弧
		AddLine(x + width - cornerX, y + height, x + cornerX, y + height); // 下边

		AddArc(x, y + height - elHei, elWid, elHei, 90, 90);
		AddLine(x, y + cornerY, x, y + height - cornerY);
	}

};





#endif // !GdiPlusRender_H_
